package com.easy.work.function.mall.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.base.syslib.Logs;
import com.base.syslib.util.MessageTools;
import com.easy.work.R;
import com.easy.work.app.BaseActivity;
import com.easy.work.function.JobFilterListAct;
import com.easy.work.function.mall.adapter.EducationAdapter;
import com.easy.work.function.mall.adapter.SexAdapter;
import com.easy.work.function.mall.adapter.WelfareAdapter;
import com.easy.work.function.mall.bean.JobCommonBean;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.EducationRequest;
import com.easy.work.http.request.JobFilterRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhangfeng
 * @date 2017/12/24
 * @desc 职位筛选
 */

public class JobFilterActivity extends BaseActivity {

    private static final String TAG = JobFilterActivity.class.getSimpleName();

    @BindView(R.id.gv_education)
    GridView gvEducation;
    @BindView(R.id.gv_sex)
    GridView gvSex;
    @BindView(R.id.gv_salary)
    GridView gvSalary;
    @BindView(R.id.gv_welfare)
    GridView gvWelfare;

    private EducationAdapter mEducationAdapter;
    private SexAdapter mSexAdapter;
    private EducationAdapter mSalaryAdapter;
    private WelfareAdapter mWelfareAdapter;

    public static void start(Context ctx) {
        Intent intent = new Intent(ctx, JobFilterActivity.class);
        ctx.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_filter);
        ButterKnife.bind(this);
        initTitle("职位筛选");

        initView();

        // 获取学历数据
        getEducationList("es.base.dictionary.EduType", "education");
        // 获取薪资数据
        getEducationList("es.base.dictionary.SalaryType", "salary");
    }

    private void initView() {
        // 学历初始化
        List<JobCommonBean.ItemsEntity> educationList = new ArrayList<>();
        mEducationAdapter = new EducationAdapter(this, 1, educationList);
        gvEducation.setAdapter(mEducationAdapter);
        gvEducation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position != mEducationAdapter.getSelect()) {
                    mEducationAdapter.updateSelect(position);
                }
            }
        });

        // 性别初始化
        List<String> sexList = new ArrayList<>();
        sexList.add("男");
        sexList.add("女");
        sexList.add("不限");
        mSexAdapter = new SexAdapter(this, 1, sexList);
        gvSex.setAdapter(mSexAdapter);
        gvSex.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position != mSexAdapter.getSelect()) {
                    mSexAdapter.updateSelect(position);
                }
            }
        });

        // 薪资初始化
        List<JobCommonBean.ItemsEntity> salaryList = new ArrayList<>();
        mSalaryAdapter = new EducationAdapter(this, 1, salaryList);
        gvSalary.setAdapter(mSalaryAdapter);
        gvSalary.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position != mSalaryAdapter.getSelect()) {
                    mSalaryAdapter.updateSelect(position);
                }
            }
        });

        // 福利初始化
        List<String> welfareList = new ArrayList<>();
        welfareList.add("包住宿");
        welfareList.add("包用餐");
        welfareList.add("缴纳社保");
        welfareList.add("免费体验");
        welfareList.add("用餐补贴");
        welfareList.add("是否双休");
        welfareList.add("班车接送");
        mWelfareAdapter = new WelfareAdapter(this, 1, welfareList);
        gvWelfare.setAdapter(mWelfareAdapter);
        gvWelfare.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mWelfareAdapter.updateSelect(position);
            }
        });

    }

    /**
     * 获取学历或者薪资数据
     *
     * @param dicId 字典
     * @param key 判断学历还是薪资
     */
    private void getEducationList(String dicId, final String key) {
        final EducationRequest request = new EducationRequest();
        request.dicId = dicId;
        request.parentKey = "";
        request.sliceType = "3";
        request.query = "";
        request.start = 0;
        request.limit = 0;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                handleData(response, key);
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
            }
        });
    }

    /**
     * 学历或薪资数据处理
     */
    private void handleData(BaseResponse response, String key) {
        JobCommonBean bean = (JobCommonBean) response;
        if (bean != null && bean.items != null && !bean.items.isEmpty()) {
            List<JobCommonBean.ItemsEntity> list = bean.items;
            if (key.equals("education")) {
                mEducationAdapter.refresh(getWrapperList(list, "学历不限"));
            } else {
                mSalaryAdapter.refresh(getWrapperList(list, "薪资不限"));
            }
        }
    }

    private List<JobCommonBean.ItemsEntity> getWrapperList(List<JobCommonBean.ItemsEntity> list, String text) {
        if (!isContainText(list, text)) {
            JobCommonBean.ItemsEntity entity = new JobCommonBean.ItemsEntity();
            entity.text = text;
            list.add(0, entity);
        }
        return list;
    }

    private boolean isContainText(List<JobCommonBean.ItemsEntity> list, String text) {
        for (JobCommonBean.ItemsEntity entity : list) {
            if (entity.text.equals(text)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 重置
     */
    @OnClick(R.id.btn_reset)
    void reset() {
        if (mEducationAdapter.getCount() > 0) {
            mEducationAdapter.updateSelect(-1);
        }
        mSexAdapter.updateSelect(-1);
        if (mSalaryAdapter.getCount() > 0) {
            mSalaryAdapter.updateSelect(-1);
        }
        mWelfareAdapter.reset();
    }

    /**
     * 确认
     */
    @OnClick(R.id.btn_sure)
    void sure() {
        int educationSelect = mEducationAdapter.getSelect();
        if (educationSelect == -1) {
            MessageTools.showToast(getApplicationContext(), "请选择学历");
            return;
        }

        int sexSelect = mSexAdapter.getSelect();
        if (sexSelect == -1) {
            MessageTools.showToast(getApplicationContext(), "请选择性别");
            return;
        }
        sexSelect = (sexSelect + 1) % 3;

        int salarySelect = mSalaryAdapter.getSelect();
        if (salarySelect == -1) {
            MessageTools.showToast(getApplicationContext(), "请选择薪资要求");
            return;
        }

        HashMap<Integer, String> map = mWelfareAdapter.getMap();
        Logs.e(TAG, "map:" + map.toString());

        final JobFilterRequest request = new JobFilterRequest();
        request.eduType = educationSelect + "";
        request.sex = sexSelect + "";
        request.salaryType = salarySelect + "";
        request.searchType = "1";
        request.encase = map.get(0);
        request.meals = map.get(1);
        request.socialInsurance = map.get(2);
        request.physicalExamination = map.get(3);
        request.subsidizedMeals = map.get(4);
        request.doubleCease = map.get(5);
        request.shuttleBus = map.get(6);
        request.start = 0;

//        request.limit = 0;

        JobFilterListAct.start(oThis, "职位筛选", request);
//        HttpClient.post(request, new HttpClient.onSuccessListener() {
//            @Override
//            public void onSuccess(BaseResponse response) {
//            }
//        }, new HttpClient.onFailListener() {
//            @Override
//            public void onFail(int code, String msg) {
//            }
//        });
    }
}
